package com.ruoyi.module.system.controller;


import com.ruoyi.core.entity.BaseMap;
import com.ruoyi.core.entity.Output;
import com.ruoyi.core.entity.PageInfo;
import com.ruoyi.flow.service.FlowFireService;
import com.ruoyi.web.annotations.ApiController;
import com.ruoyi.web.annotations.ApiLog;
import com.ruoyi.web.domain.SysMod;
import com.ruoyi.web.entity.system.FireVo;
import com.ruoyi.web.service.SysModDataService;
import com.ruoyi.web.service.SysModFactory;
import com.ruoyi.web.service.SysModService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@ApiController
@ApiLog
@RestController
@RequestMapping("/sysModData")
public class SysModDataController {

    @Autowired
    private SysModService sysModService;

    @Autowired
    private FlowFireService flowFireService;

    private SysModDataService getService(BaseMap entity) {
        String modId = null;
        if (entity.containsKey("mid")) {
            modId = entity.getString("mid");
        } else if (entity.containsKey("mod_id")) {
            modId = entity.getString("mod_id");
        }
        return SysModFactory.getSysModDataService(modId);
    }

    private SysModDataService getService(String modId) {
        return SysModFactory.getSysModDataService(modId);
    }


    // ===========查询接口============
    @PostMapping("/getPage")
    public Output<PageInfo<BaseMap>> getPage(@RequestBody BaseMap entity) {

        if (entity == null) {
            entity = new BaseMap();
        }

        PageInfo<BaseMap> page = getService(entity).page(entity.getString("modId"), entity);

        return Output.success(page);

    }

    @PostMapping("/getList")
    public Output<List<BaseMap>> getList(@RequestBody BaseMap entity) {

        if (entity == null) {
            entity = new BaseMap();
        }

        List<BaseMap> list = getService(entity).list(entity.getString("modId"), entity);

        return Output.success(list);

    }

    //===========操作接口===========
//    @PostMapping("/add")
//    public Output<BaseMap> add(@RequestBody BaseMap entity) {
//        return Output.success(getService(entity).add(entity));
//    }
//
//    @PostMapping("/edit")
//    public Output<BaseMap> edit(@RequestBody BaseMap entity) {
//        return Output.success(getService(entity).edit(entity));
//    }
//
//    @PostMapping("/delete")
//    public Output<Object> delete(@RequestBody BaseMap entity) {
//        getService(entity).delete(entity);
//        return Output.success();
//    }

    @PostMapping("/fire")
    public Output<Object> fire(@RequestBody FireVo fireVo) {

        String modId = fireVo.getModId();

        SysMod sysMod = sysModService.getById(fireVo.getModId());

        if ("1".equals(sysMod.getIsFlow())) { // 有流程

            com.ruoyi.flow.entity.vo.FireVo fireVo1 = new com.ruoyi.flow.entity.vo.FireVo();
            BeanUtils.copyProperties(fireVo, fireVo1);
            fireVo1.setData(fireVo.getData());
            flowFireService.fire(fireVo1);
            return Output.success();

        } else {
            String eventId = fireVo.getEventId();
            if ("add".equalsIgnoreCase(eventId)) {
                return Output.success(getService(modId).add(modId, fireVo.getData()));
            } else if ("edit".equalsIgnoreCase(eventId)) {
                return Output.success(getService(modId).edit(modId, fireVo.getData()));
            } else if ("delete".equalsIgnoreCase(eventId)) {
                getService(modId).delete(modId, fireVo.getData());
                return Output.success();
            }
        }

        return null;
    }

}
